package com.itboye.mengwu.app.ui.main;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.XImageLoader;
import com.itboye.core.utils.XPicasso;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.web.WebActivity;
import com.itboye.mengwu.data.model.AppHeadPage;
import com.itboye.mengwu.data.presenter.HomePagePresenter;

public class ActivityGlobalShop extends BaseActivity implements Observer {
	AppHeadPage page;
	ArrayList<String> imagesList = new ArrayList<String>();
	ArrayList<String> goodsName = new ArrayList<String>();
	ArrayList<String> goodsPrice = new ArrayList<String>();
	ArrayList<String> goodsPid = new ArrayList<String>();
	HomePagePresenter homePagePresenter;
	ArrayList<AppHeadPage> arrayList = new ArrayList<AppHeadPage>();
	LinearLayout list;
	ImageView ad_two;
	String adtwo;
	String blokdan2;
	String blok4;
	ArrayList<String> bannerNamedan = new ArrayList<String>();
	ArrayList<String> bannerNamedan2 = new ArrayList<String>();
	String APP_URL = "http://www.moodwo.com/index.php/Webview";
	TextView txt_title;
	ImageView back;
	int posionID;

	public ActivityGlobalShop() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_globalshop;
	}

	// HomePageSecondAdapter adapter;

	// private ArrayList<GridViewBean> list = new ArrayList<GridViewBean>();

	@Override
	public void onMyCreate() {
		homePagePresenter = new HomePagePresenter(this);
		// posionID =
		String ss = getIntent().getStringExtra("page_name");
		posionID = Integer.parseInt(getIntent().getStringExtra("page_id"));
		Log.e("hu...gol", posionID + "");
		txt_title.setText(ss);
		if (posionID != 0) {
			homePagePresenter.appHead("" + posionID);
		}
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@SuppressWarnings("deprecation")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity model = super.handlerError(data);
		if (model != null) {
			if (model.getEventType() == HomePagePresenter.BannerSuccessEvent) {
				page = (AppHeadPage) model.getData();
				for (int j = 0; j < page.getItem_data().size(); j++) {
					String dataString = (String) page.getItem_data().get(j)
							.getData();
					try {
						JSONObject jsonObject = new JSONObject(dataString);
						String block_id = jsonObject.getString("block_id");
						String block_name = jsonObject.getString("block_name");
						JSONArray bannArray = jsonObject
								.getJSONArray("banners");
						for (int i = 0; i < bannArray.length(); i++) {
							JSONObject banners = bannArray.getJSONObject(i);
							int key = banners.getInt("key");
							String id = banners.getString("id");
							String name = banners.getString("name");
							// int width = banners.getInt("width");
							// int height = banners.getInt("height");
							// JSONObject groupJsonObject = banners
							// .getJSONObject("product_group");

							JSONObject grouplistJsonObject = banners
									.getJSONObject("group_product_list");

							// String max = groupJsonObject.getString("max");
							// ArrayList<String> product_ids =
							// (ArrayList<String>)
							// groupJsonObject
							// .get("product_ids");

							// String[] pidsString = grouplistJsonObject
							// .get("p_ids");

							// JSONArray skuArray = grouplistJsonObject
							// .getJSONArray("sku");
							// JSONObject skuJsonObject =
							// skuArray.getJSONObject(0);
							// String keyString =
							// skuJsonObject.getString("key");
							// String idString = skuJsonObject.getString("id");
							// String nameString =
							// skuJsonObject.getString("name");
							// String skuid = skuJsonObject.getString("sku_id");
							// String end_time =
							// skuJsonObject.getString("end_time");
							// String start_time =
							// skuJsonObject.getString("start_time");
							// String price = skuJsonObject.getString("price");
							// String indexForSort =
							// skuJsonObject.getString("indexForSort");
							// String display_order = skuJsonObject
							// .getString("display_order");
							if (j == 0) {
								adtwo = id;
								bannerNamedan2.add(block_name);
								blokdan2 = block_id;
								JSONObject producthomeObject = banners
										.getJSONObject("product_home");
								JSONArray productInfoArray = producthomeObject
										.getJSONArray("product_info");
								for (int k = 0; k < productInfoArray.length(); k++) {
									JSONObject productObject = productInfoArray
											.getJSONObject(k);
									String product_id = productObject
											.getString("product_id");
									String pname = productObject
											.getString("name");
									String img = productObject.getString("img");
									String price = productObject
											.getString("price");
									goodsName.add(pname);
									goodsPrice.add(price);
									goodsPid.add(product_id);
									imagesList.add(img);
									// key.length();
								}

							}

						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// ad_two.setScaleType(ScaleType.CENTER_CROP);
					// showImage(adtwo, ad_two);
					XPicasso.loadImage(this, ad_two, GlobalConfig.API_URL
							+ "/picture/index?id=" + adtwo);
					ad_two.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent2 = new Intent(
									ActivityGlobalShop.this, WebActivity.class);
							intent2.putExtra("title", bannerNamedan2.get(0));
							intent2.putExtra("url", APP_URL
									+ "/viewPage/index/block/{" + blokdan2
									+ "}/banner/{" + 0 + "}");
							startActivity(intent2);
						}
					});

					for (int i = 0; i < goodsName.size(); i++) {

						TextView[] pname = new TextView[goodsName.size()];
						TextView[] price = new TextView[goodsPrice.size()];
						ImageView[] img = new ImageView[imagesList.size()];
						View view = LayoutInflater.from(this).inflate(
								R.layout.topview, null);
						ImageView imageView = (ImageView) view
								.findViewById(R.id.hot);
						TextView textView = (TextView) view
								.findViewById(R.id.tv1);
						TextView textView2 = (TextView) view
								.findViewById(R.id.tv2);
						textView.setText(goodsName.get(i));
						textView2.setText("￥" + goodsPrice.get(i));
						showImage(imagesList.get(i), imageView);
						textView.setGravity(Gravity.CENTER);
						textView.setTextColor(getResources().getColor(
								R.color.black));
						img[i] = imageView;
						img[i].setTag(i);
						android.widget.LinearLayout.LayoutParams lP = new android.widget.LinearLayout.LayoutParams(
								android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
								android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
						lP.setMargins(10, 10, 10, 10);
						list.addView(view, lP);
						img[i].setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								Intent intent = new Intent(
										ActivityGlobalShop.this,
										GoodsDetailActivity.class);
								intent.putExtra("pid",
										goodsPid.get((int) v.getTag()));
								startActivity(intent);
							}
						});

						// list.
					}
				}
			}
		}
	}

	private void showImage(String imageid2, ImageView ad_one2) {
		// TODO Auto-generated method stub
		XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
				+ imageid2, ad_one2);
	}

}
